package com.ruoyi.web.controller.panel;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.ICusUserService;
import com.ruoyi.system.service.IPanelGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/panel/group")
public class PanelGroupController extends BaseController {

    @Autowired
    private IPanelGroupService panelGroupService;
    @Autowired
    private ICusUserService cusUserService;

    private List<WaUser> waUserList = null;

    private int requestCount = 0; // 添加一个计数器来跟踪完成的请求数量

    private boolean isRequestInProgress = false; // 标志表示是否有请求正在进行


    @PreAuthorize("@ss.hasPermi('chat:ui:list')")
    @GetMapping("/total")
    public AjaxResult getPanelTotal() {
//        if (isRequestInProgress) {
//            waUserList = null; // 如果检测到重复请求，重置waUserList
//        }
//        isRequestInProgress = true;
//        List<WaUser> list = getWaUserList();
//        PanelTotal panelTotal = panelGroupService.getPanelTotal(list);
        return success(0);
    }

    @PreAuthorize("@ss.hasPermi('chat:ui:list')")
    @GetMapping("/toDayTotal")
    public AjaxResult getToDayPanelTotal() {
//        if (isRequestInProgress) {
//            waUserList = null; // 如果检测到重复请求，重置waUserList
//        }
//        isRequestInProgress = true;
//        List<WaUser> list = getWaUserList();
//        PanelTotal panelTotal = panelGroupService.getTodayPanelTotal(list);
        return success(0);
    }

    @PreAuthorize("@ss.hasPermi('chat:ui:list')")
    @GetMapping("/list")
    public TableDataInfo getList() {
//        if (isRequestInProgress) {
//            waUserList = null; // 如果检测到重复请求，重置waUserList
//        }
//        isRequestInProgress = true;
//        startPage();
//        List<WaUser> list = getWaUserList();
//        List<UserConversionsTotal> userList = panelGroupService.getUserConversionsTotal(list);
        return getDataTable(null);
    }

    public List<WaUser> getWaUserList() {
        if (waUserList == null) {
//            String userName = getUsername();
            CustomerInfo user = new CustomerInfo();
            List<CustomerInfo> userlist = cusUserService.selectUserList(user);
//            waUserList = cusUserService.selectCustomerChatList(getUsername(), userlist);
        }
        return waUserList;
    }

//    public List<WaUser> getWaUserList() {
//        if (waUserList == null) {
//            String userName = getUsername();
////            CusUser user = new CusUser();
//            List<CusUser> userlist = cusUserService.selectUserByName(userName);
//            waUserList = cusUserService.selectCustomerChatList(getUsername(), userlist);
//        }
//        return waUserList;
//    }

    private void checkAndReset() {
        requestCount++;
        if (requestCount == 3) { // 如果三个请求都已完成
            waUserList = null; // 重置waUserList
            requestCount = 0; // 重置请求计数器
        }
    }


}
